package com.blandware.atleap.webapp.action.news;

import com.blandware.atleap.common.Constants;
import com.blandware.atleap.common.NewsModuleConstants;
import com.blandware.atleap.common.util.QueryInfo;
import com.blandware.atleap.model.core.User;
import com.blandware.atleap.model.news.NewsItem;
import com.blandware.atleap.service.core.UserManager;
import com.blandware.atleap.service.news.NewsManager;
import com.blandware.atleap.webapp.util.core.GlobalProperties;
import com.blandware.atleap.webapp.util.core.WebappConstants;
import com.blandware.atleap.webapp.util.core.WebappUtil;
import com.blandware.atleap.webapp.util.news.NewsModuleWebConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.ControllerSupport;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;


/**
 * <p>NewsListController class.  This class is used to display list of news items
 * </p>
 * <p><a href="NewsListController.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.17 $ $Date: 2006/08/03 10:08:04 $
 */
public final class NewsListController extends ControllerSupport {

	protected transient final Log log = LogFactory.getLog(NewsListController.class);

	//~ Methods ================================================================
	/**
	 * Retrieves all menu items and puts them into request
	 *
	 * @param tilesContext   Current tile context
	 * @param request        Current request
	 * @param response       Current response
	 * @param servletContext Current Servlet Context
	 */
	public void execute(ComponentContext tilesContext,
	                    HttpServletRequest request,
	                    HttpServletResponse response,
	                    ServletContext servletContext) throws Exception {

		ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

		Integer limit = GlobalProperties.getInstance(request.getSession().getServletContext()).getInteger(NewsModuleWebConstants.NEWS_LIST_SIZE, new Integer(5));

		List roles = null;
		String userName = request.getRemoteUser();
		UserManager userManager = (UserManager) applicationContext.getBean(Constants.USER_MANAGER_BEAN);
		if ( userName != null ) {
			User user = userManager.retrieveUser(userName);
			roles = user.getRoles();
		}

		Locale locale = (Locale) request.getSession().getAttribute(Globals.LOCALE_KEY);

		Map queryParameters = new HashMap();
		queryParameters.put("localeIdentifier", locale.getLanguage());
		queryParameters.put("roles", roles);

		QueryInfo queryInfo = new QueryInfo();
		queryInfo.setQueryParameters(queryParameters);
		queryInfo.setLimit(limit);

		NewsManager newsManager = (NewsManager) applicationContext.getBean(NewsModuleConstants.NEWS_MANAGER_BEAN);
		List newsList = newsManager.getLastNews(queryInfo);

		// set URI
		for ( ListIterator i = newsList.listIterator(); i.hasNext(); ) {
			NewsItem newsItem = (NewsItem) i.next();
			NewsItem tmp = new NewsItem();
			WebappUtil.copyProperties(tmp, newsItem, request);
			tmp.setUri(WebappUtil.getActionMappingURL(newsItem.getUri(), null, request, WebappConstants.URL_TYPE_CONTEXT_RELATIVE));
			i.set(tmp);
		}

		request.setAttribute(NewsModuleWebConstants.NEWS_ITEMS_COLLECTION_KEY, newsList);
	}
}
